package com.qs.eggs.view.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.qs.eggs.Constants;
import com.qs.eggs.R;
import com.qs.eggs.adapter.AddressAdapter;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Response;

public class AddressManagementActivity extends Activity implements View.OnClickListener{
    private RelativeLayout fanhui;
    private TextView tv_add;
    private  RecyclerView recycleView;
    private  Button add;
    private AddressAdapter addressAdapter;
    private List<Map<String, Object>> addlist= null;
    public static Activity instance;
    private String biaoshi;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        instance=this;
        setContentView(R.layout.activity_address_management);
        biaoshi=getIntent().getStringExtra("biaoshi");
        initView();
        getAddress();
    }
    private void initView(){
        fanhui=(RelativeLayout)findViewById(R.id.fanhui);
        tv_add=(TextView)findViewById(R.id.tv_add);
        recycleView=(RecyclerView)findViewById(R.id.recycleView);
        add=(Button)findViewById(R.id.add);
        fanhui.setOnClickListener(this);
        tv_add.setOnClickListener(this);
        add.setOnClickListener(this);
        recycleView.setLayoutManager(new LinearLayoutManager(this));
   }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.fanhui:
           AddressManagementActivity.this.finish();
             break;
            case R.id.tv_add:
                AddressManagementActivity.this.finish();
                Intent intent=new Intent(this,AddAddressActivity.class);
                startActivity(intent);
                break;
            case R.id.add:
                Intent intent1=new Intent(this,AddAddressActivity.class);
                startActivity(intent1);
                break;
     }
        }
    //获得地址信息
    private void getAddress(){
        OkGo.post(Constants.URL_BASE+"myorder?action=searchAddress")
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        System.out.print("地址查询=="+response);
                        try {
                            addlist=new ArrayList<>();
                            JSONArray jsonArray = new JSONArray(s);
                            for (int i = 0; i < jsonArray.length(); i++) {
                                JSONObject jsonObject = jsonArray.getJSONObject(i);
                                Map<String, Object> map = new HashMap<>();
                                map.put("custname", jsonObject.getString("custname"));
                                map.put("phone", jsonObject.getString("phone"));
                                map.put("address", jsonObject.getString("address"));
                                map.put("custid", jsonObject.getString("custid"));
                                map.put("isdefault", jsonObject.getString("isdefault"));
                                map.put("id", jsonObject.getString("id"));
                                map.put("province", jsonObject.getString("province"));
                                map.put("city", jsonObject.getString("city"));
                                map.put("area", jsonObject.getString("area"));
                                map.put("village", jsonObject.getString("village"));
                                map.put("provinceid", jsonObject.getString("provinceid"));
                                map.put("cityid", jsonObject.getString("cityid"));
                                map.put("areaid", jsonObject.getString("areaid"));
                                map.put("villageid", jsonObject.getString("villageid"));
                                addlist.add(map);
                            }

                            addressAdapter = new AddressAdapter(AddressManagementActivity.this, addlist);
                            recycleView.setAdapter(addressAdapter);
                            if (biaoshi.equals("1")) {
                                addressAdapter.setOnItemClickListener(new AddressAdapter.OnItemClickListener() {
                                    @Override
                                    public void onItemClick(View view, int position) {
                                        String address=addlist.get(position).get("province").toString()+addlist.get(position).get("city").toString()
                                                +addlist.get(position).get("area").toString()+addlist.get(position).get("village").toString()+addlist.get(position).get("address").toString();
                                        Intent intent=new Intent(AddressManagementActivity.this,SubmitOrderActivity.class);
                                        intent.putExtra("address",address );
                                        setResult(99,intent);
                                        finish();
                                    }
                                });
                            }else{

                            }
                        }catch(Exception e){
                            e.printStackTrace();
                        }
                    }

                });

 }
}
